var Page = function(){
    this.init();
};
Page.prototype = {
    params: {
        passport: '',
        releaseStatus:1,
        pushType:'text'
    },
    init: function(){
        this.initParams();
        this.initEvent();
    },
    initParams: function(){
        this.params.passport = Pub.getCookie('_PASSPORT');
    },
    getallclient:function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport
        };
        Api.getallclient(paramsObj, function(data){
            console.log(data);
            _this.getallclientList(data);

        });
    },
    getallclientList:function(data){
        var _this = this;
        if (data) {
            var html = '<div class="goods-checkbox mr-5">'+
                            '<div class="check-box">'+
                                '<input type="checkbox" name="all">'+
                            '</div>'+
                        '</div>'+
                        '<span class="mr-10">全部</span>';
            for(i in data){
              html +='<div class="goods-checkbox mr-5">'+
                            '<div class="check-box">'+
                                '<input type="checkbox" keys="'+i+'" name="client">'+
                            '</div>'+
                        '</div>'+
                        '<span class="mr-10">'+data[i]+'</span>';

            }

            $('#clientList').html(html);
            _this.getallclientEvent();
        } else {
            var html = '<div>暂无数据</div>'

            $('#clientList').html(html);
            $('input').iCheck({
                checkboxClass: 'icheckbox-blue',
                radioClass: 'iradio-blue',
                increaseArea: '20%'
            })
        }
    },
    getallclientEvent:function(){
        var _this = this;
        $('input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })
        $('input[type=checkbox][name=all]').on('ifChanged', function() {
            if ($(this).is(':checked')) {
                $('input[type=checkbox][name=client]').iCheck('check');
            } else {
                $('input[type=checkbox][name=client]').iCheck('uncheck');
            }
        });
    },
    submit:function(){
        var _this = this;
        $('#submit').click(function(event) {
            var title=$('#title').val();
            var desc=$('#desc').val();
            var subscribeTime=$('#times').val();
            if (desc=='') {
                desc=title;
            };
            var content=ue.getContent();
            var url=$('#Url').val();
            var remark=$('#remark').val();
            var isPush=0;
            if ($('#isPush').hasClass('switch-on')) {
                isPush=1;
            };
            var arr=[];
            var client='';
            if ($('input[name="client"]:checked').length>0) {
                for (var i = 0; i < $('input[name="client"]:checked').length; i++) {
                    arr.push($('input[name="client"]:checked').eq(i).attr('keys'));
                };
                client=arr.join(",");
            };
            var paramsObj = {
                passport: _this.params.passport,
                client:client,
                title:title,
                desc:desc,
                releaseStatus:_this.params.releaseStatus,
                subscribeTime:subscribeTime,
                isPush:isPush,
                pushType:_this.params.pushType,
                content:content,
                url:url,
                remark:remark,
            };
            console.log(paramsObj);
            Api.releasesystemmessage(paramsObj, function(data){
                console.log(data);
                layui.use('layer', function(){
                    layer.msg('发布成功', {icon: 6});
                    setTimeout(function(){
                        window.location.href = Pub.getHtmlUrl('./message-list.html');
                    },1500)
                });

            });

        });
    },
    otherEvent:function(){
        var _this = this;
        layui.use('laydate', function(){
          var laydate = layui.laydate;
          laydate.render({
            elem: '#times',
            type: 'datetime'
          });
        });
        $('input[name="line"]').on('ifChanged', function() {
           if ($(this).is(':checked')) {
                if ($(this).val()==0) {
                    $('.timeWrap').hide();
                    _this.params.releaseStatus=0;
                }else if($(this).val()==1){
                    $('.timeWrap').hide();
                    _this.params.releaseStatus=1;
                }else if($(this).val()==2){
                    $('.timeWrap').show();
                    _this.params.releaseStatus=2;
                };
            }
        });
        $('input[name="types"]').on('ifChanged', function() {
           if ($(this).is(':checked')) {
                if ($(this).val()==0) {
                    $('.editorWrap').hide();
                    $('.h5Wrap').show();
                    _this.params.pushType='url';
                }else{
                    $('.editorWrap').show();
                    $('.h5Wrap').hide();
                    _this.params.pushType='text';
                };
            }
        });
    },
    initEvent: function(){
        var _this = this;

        _this.otherEvent();
        _this.submit()
        _this.getallclient();

    }
};
var page = new Page();